"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _system = require("../../static/utils/system.js");

var _system2 = _interopRequireDefault(_system);

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : {
    default: obj
  };
}

var app = getApp();
exports.default = Page({
  data: {
    isAuthorize: false,
    status: "0",
    netWorkType: ""
  },
  onLoad: function onLoad(e) {
    var url = e.url || "";
    var mid = e.mid || "";
    var userid = e.id || e.userid;
    var isShare = wx.getStorageSync("cid") || "";
    var openid = wx.getStorageSync("openid") || "";
    var phone = wx.getStorageSync('phone');
    var phoneinfo = wx.getStorageSync('phoneinfo');
    if (url) {
      wx.navigateTo({
        url: url + "?mid=" + mid + "&userid=" + userid
      });
    }
    if (!isShare) {
      if (userid) {
        wx.setStorageSync("cid", userid);
      }
    }
    if (!phone) {
      var _this = this
      var host = "https://api.bjlya.com.cn/xcx/app.php?";
      var data = {
        mod: "do",
        act: "tips",
        ssid: wx.getStorageSync('openid') || ''
      };
      wx.request({
        url: host,
        data: data,
        method: "GET",
        header: {
          "content-type": "application/json"
        },
        success: function success(result) {
          // console.log(result)
          var info = result.data.info
          _this.setData({
            baseinfo: info.baseinfo
          })
        }
      })
    }
    /*if (!openid) {
      var _this = this;
      _this.setData({
        isAuthorize: true
      });
    }*/
  },
  onShow: function onShow() {
    var _this = this;
    var openid = wx.getStorageSync("openid") || "";
    wx.getNetworkType({
      success: function success(res) {
        _this.setData({
          netWorkType: res.networkType
        });
      }
    });
    if (!openid) {} else {
      var data = {
        mod: "do",
        act: "info",
        ssid: openid
      };
      _system2.default.getUserInfo(data, "post");
      setTimeout(function() {
        _this.setData({
          openid: openid,
          info: wx.getStorageSync("info")
        });
      }, 1000);
    } {
      var host = app.globalData.host;
      var _openid = _this.data.openid || wx.getStorageSync("openid") || "";
      var _data = {
        mod: "product",
        act: "list",
        ssid: _openid,
        mid: 0,
        opt: "xxk",
        ifbuy: 0,
        key: ""
      };
      wx.request({
        url: host,
        data: _data,
        method: "post",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function success(res) {
          _this.setData({
            status: "0",
            openid: _openid,
            proList: res.data.info
          });
        }
      });
    } {
      var _host = app.globalData.host;
      var _openid2 = _this.data.openid || wx.getStorageSync("openid") || "";
      var _data2 = {
        mod: "category",
        act: "list",
        ssid: _openid2,
        mid: "xxk"
      };
      wx.request({
        url: _host,
        data: _data2,
        method: "post",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function success(res) {
          _this.setData({
            banner: res.data.tab,
            header: res.data.info
          });
        }
      });
    }
  },
  onHide: function onHide() {
    this.setData({
      isAuthorize: false
    });
  },
  onReady: function onReady() {},
  onShareAppMessage: function onShareAppMessage(res) {
    // var _this = this;
    var info = wx.getStorageSync("info");
    if (info) {
      var userid = this.data.info.id;
      return {
        title: "门品学社",
        desc: "当下最具人气的小程序！",
        path: "/pages/offline/offline?id=" + userid
      };
    } else {
      return {
        title: "门品学社",
        desc: "当下最具人气的小程序！",
        path: "/pages/offline/offline"
      };
    }
  },
  refuse: function refuse() {
    var _this = this;
    _this.setData({
      isAuthorize: false
    });
    // wx.switchTab({
    //   url: "/pages/offline/offline"
    // });
  },
  login: function login(info) {
    var _this = this;
    var iv = info.detail.iv;
    var userInfo = info.detail.userInfo;
    var encryptedData = info.detail.encryptedData;
    if (userInfo) {
      wx.login({
        success: function success(response) {
          var code = response.code;
          var host = "https://api.bjlya.com.cn/xcx/app.php?";
          var data = {
            mod: "do",
            act: "openid",
            mid: code,
            iv: iv,
            encryptedData: encryptedData,
            cid: wx.getStorageSync("cid") || ""
          };
          wx.request({
            url: host,
            data: data,
            method: "GET",
            header: {
              "content-type": "application/json"
            },
            success: function success(result) {
              // console.log(result);
              wx.setStorageSync("type", result.data.info.type);
              wx.setStorageSync("openid", result.data.info.ssid);
              wx.setStorageSync("session_key", result.data.info.session);
              _this.setData({
                isAuthorize: false
              });
              var method = "post";
              var host = "https://api.bjlya.com.cn/xcx/app.php?";
              var data = {
                mod: "do",
                act: "info",
                ssid: result.data.info.ssid
              };
              if (method == "post") {
                wx.request({
                  url: host,
                  data: data,
                  method: method,
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  success: function success(res) {
                    wx.setStorageSync("info", res.data.info);
                  }
                });
              } else if (method == "get") {
                wx.request({
                  url: host,
                  data: data,
                  method: method,
                  header: {
                    "Content-Type": "application/json"
                  },
                  success: function success(res) {
                    wx.setStorageSync("info", res.data.info);
                  }
                });
              }
            }
          });
        },
        fail: function fail(res) {
          // wx.switchTab({
          //   url: "/pages/offline/offline"
          // });
        }
      });
    } else {
      // wx.switchTab({
      //   url: "/pages/offline/offline"
      // });
    }
    wx.showToast({
      icon: "loading",
      duration: 1000,
      mask: false,
      success: function success() {
        setTimeout(function() {
          _this.setData({
            openid: wx.getStorageSync("openid"),
            info: wx.getStorageSync("info"),
            isAuthorize: false
          });
        }, 1000);
      }
    });
  },
  search: function search(e) {
    var _this = this;
    var host = app.globalData.host;
    var input = e.detail.value.input;
    var data = {
      mod: "product",
      act: "list",
      ssid: _this.data.openid || "",
      mid: 0,
      opt: "xxk",
      ifbuy: 0,
      key: input
    };
    wx.request({
      url: host,
      data: data,
      method: "post",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function success(res) {
        _this.setData({
          status: "0",
          proList: res.data.info
        });
      }
    });
  },
  setStyle: function setStyle(e) {
    var _this = this;
    var openid = _this.data.openid;
    var type = e.currentTarget.dataset.class;
    var mid = e.currentTarget.dataset.id;
    var host = app.globalData.host;
    var length = _this.data.header.length;
    if (type != "0" && type != length) {
      var data = {
        mod: "product",
        act: "list",
        ssid: _this.data.openid || "",
        mid: mid,
        opt: "xxk",
        ifbuy: 0,
        key: ""
      };
      wx.request({
        url: host,
        data: data,
        method: "post",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function success(res) {
          _this.setData({
            status: type,
            proList: res.data.info
          });
        }
      });
    } else {
      if (type != length) {
        var _data3 = {
          mod: "product",
          act: "list",
          ssid: _this.data.openid || "",
          mid: type,
          opt: "xxk",
          ifbuy: 0,
          key: ""
        };
        wx.request({
          url: host,
          data: _data3,
          method: "post",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function success(res) {
            _this.setData({
              status: type,
              proList: res.data.info
            });
          }
        });
      } else {
        if (openid) {
          var _data4 = {
            mod: "product",
            act: "list",
            ssid: _this.data.openid || "",
            mid: 0,
            opt: "xxk",
            ifbuy: 1,
            key: ""
          };
          wx.request({
            url: host,
            data: _data4,
            method: "post",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function success(res) {
              _this.setData({
                status: type,
                proList: res.data.info
              });
            }
          });
        } else {
          _this.setData({
            isAuthorize: true
          });
        }
      }
    }
  },
  linkOfflineDetail: function linkOfflineDetail(e) {
    var prop = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "detail/detail?id=" + prop
    });
  },
  linkBanner: function linkBanner(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      wx.navigateTo({
        url: url
      });
    } else {
      wx.showToast({
        title: "找不到指定的路径",
        icon: "none",
        duration: 1000,
        mask: false
      });
    }
  }
});